package ferp.android.social.twvk.tw;

import android.app.Activity;
import android.view.View;
import android.widget.ImageView;
import ferp.android.R;
import ferp.android.dialogs.ShareDialog;
import ferp.android.managers.ProfileManager;
import ferp.android.social.Post;
import ferp.android.social.twvk.Account;
import ferp.android.social.twvk.TwVkConnector;
import ferp.core.log.Log;
import twitter4j.Status;
import twitter4j.Twitter;
import twitter4j.TwitterException;
import twitter4j.TwitterFactory;
import twitter4j.auth.AccessToken;
import twitter4j.conf.Configuration;
import twitter4j.conf.ConfigurationBuilder;

import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * User: olegs
 * Date: 5/14/13 10:28 AM
 */
public class TwConnector extends TwVkConnector
{
  static final String TAG = Log.TAG + "/Tw";

  public TwConnector()
  {
    ConfigurationBuilder builder = new ConfigurationBuilder();

    builder.setOAuthConsumerKey(TWITTER_CONSUMER_KEY);
    builder.setOAuthConsumerSecret(TWITTER_CONSUMER_SECRET);

    Configuration configuration = builder.build();

    factory = new TwitterFactory(configuration);
  }

  @Override
  public int getRequestCode()
  {
    return RequestCode.TW;
  }

  @Override
  public ImageView getLogoutIcon(ShareDialog dialog)
  {
    return (ImageView)(dialog.findViewById(R.id.social_frame_layout_tw).findViewById(R.id.dlg_share_logout));
  }

  @Override
  public ImageView getProgressIcon(ShareDialog dialog)
  {
    return (ImageView)(dialog.findViewById(R.id.social_frame_layout_tw).findViewById(R.id.dlg_share_clock));
  }

  @Override
  public ImageView getPostToastIcon(View toast)
  {
    return (ImageView)toast.findViewById(R.id.toast_post_result_tw);
  }

  @Override
  public ImageView getLogoutToastIcon(View toast)
  {
    return (ImageView)toast.findViewById(R.id.toast_social_logout_tw);
  }

  @Override
  protected void doLogin(Activity owner, int requestCode)
  {
    twitter = null;

    super.doLogin(owner, requestCode);
  }

  @Override
  protected Class getLoginActivityClass()
  {
    return TwLoginActivity.class;
  }

  @Override
  protected PostError publish(Activity owner, Post post)
  {
    try
    {
      if (twitter == null)
      {
        Account a = loadAccount(owner);
        twitter = factory.getInstance(new AccessToken(a.accessToken, a.secretID));
      }

      StringBuilder sb = new StringBuilder();

      sb.append(post.getText(owner.getResources().getString(R.string.post_playing_in_preferans))).
         append(Post.link(ProfileManager.instance(owner).getCurrentProfile().settings.locale)).
         append('\n').append(sdf.format(new Date()));

      Status status = twitter.updateStatus(sb.toString());

      Log.debug(TAG, "Publish succeeded, status ID: " + status.getId());

      return null;
    }
    catch (TwitterException e)
    {
      Log.debug(TAG, "Publish failed (" + e.getErrorCode() + ", '" + e.getErrorMessage() + "')");
      return new PostError(-1, e.getErrorMessage());
    }
    catch (Exception e)
    {
      Log.error(TAG, e);
      return new PostError(-2, e.getMessage());
    }
  }

  @Override
  protected void doLogout(Activity owner)
  {
    twitter = null;
  }

  // twitter application data
  static String TWITTER_CONSUMER_KEY = "YUNBl3kpBQEWViFNWOl2zQ";
  static String TWITTER_CONSUMER_SECRET = "NLknLtzW5EeNzAi1T0fVIWZ28b4Bs0MbpPyzPf8qFzY";

  // authenticated callback url
  static final String TWITTER_CALLBACK_URL = "oauth://t4jsample";

  // Twitter oauth urls
  private static final String URL_TWITTER_AUTH = "auth_url";
  static final String URL_TWITTER_OAUTH_VERIFIER = "oauth_verifier";
  private static final String URL_TWITTER_OAUTH_TOKEN = "oauth_token";

  private static final SimpleDateFormat sdf = new SimpleDateFormat("dd MMM yyyy HH:mm:ss");

  private TwitterFactory factory;
  private Twitter twitter;
}
